﻿using BLL;
using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ManagerDao
    {
        public List<Admin> list(Admin admin)
        {
            // 定义SQL
            string sql = "select * from Admin where 1 = 1";
            if (admin.UserName != null && admin.UserName != "")
            {
                sql += " and UserName = @UserName ";
            }
            if (admin.PersonName != null && admin.PersonName != "")
            {
                sql += " and  PersonName  like @PersonName ";
            }
            SqliteParameter[] sp = {
                new SqliteParameter("PersonName", "%" + admin.PersonName + "%"),
                new SqliteParameter("UserName", admin.UserName),
            };
            // 执行SQL
            List<List<Dictionary<string, object>>> data = SQLiteHelper.ExecuteMap(sql, sp);
            // 数据库 转 MODEL对象
            List<Admin> list = SQLiteHelper.DataToModel<Admin>(data);
            return list;
        }

        public int setAdmin(Admin admin)
        {
            string sql = "update  Admin set IsSuperAdmin = @IsSuperAdmin where  PersonId = @PersonId";
            SqliteParameter[] sp = {
                new SqliteParameter("PersonId", admin.PersonID),
                new SqliteParameter("IsSuperAdmin", "是")
            };
            return SQLiteHelper.ExecuteNonQuery(sql, sp);
        }

        public int deleteById(string personId)
        {
            string sql = "delete from Admin where  PersonId = @PersonId";
            SqliteParameter[] sp = {
                new SqliteParameter("PersonId", personId)
            };
            return SQLiteHelper.ExecuteNonQuery(sql, sp);
        }

        public int save(Admin admin)
        {
            string sql = "insert into  Admin values(@PersonId,@PersonName,@UserName,@Password,@Email,@IsSuperAdmin)";
            SqliteParameter[] sp = {
                new SqliteParameter("PersonId", admin.PersonID),
                new SqliteParameter("PersonName", admin.PersonName),
                new SqliteParameter("UserName", admin.UserName),
                new SqliteParameter("Password", admin.Password),
                new SqliteParameter("Email", admin.Email),
                new SqliteParameter("IsSuperAdmin", admin.IsSuperAdmin)
            };
            return SQLiteHelper.ExecuteNonQuery(sql, sp);
        }


    }
}
